<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\SeekService;
use Illuminate\Http\Request;

class SeekController extends BaseController
{
    //咨询

    public function __construct(SeekService $service)
    {
        $this->db = $service;
    }

    //咨询创建
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $doctor_id = $request->post('doctor_id', 0);
        $seek_id = $request->post('seek_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->getOne($doctor_id, $seek_id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //获取内容列表
    public function getCntList(Request $request)
    {
        $zxid = $request->post('zx_id', 0);
        $res = $this->db->getCntList($zxid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //医生发送
    public function doctorSend(Request $request)
    {
        $token = $request->post('token', '');
        $zxid = $request->post('zx_id', 0);
        $cnt = $request->post('cnt', '');
        $did = $this->db->uidByToken($token);
        if (!$did) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$zxid || !$cnt) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->doctorAdd($zxid, $did, $cnt);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //病人发送
    public function patientSend(Request $request)
    {
        $token = $request->post('token', '');
        $zxid = $request->post('zx_id', 0);
        $cnt = $request->post('cnt', '');
        $patid = $this->db->uidByToken($token);
        if (!$patid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$zxid || !$cnt) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->patientAdd($zxid, $patid, $cnt);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 患者病例列表
     */
    public function patBingliList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->patBingliList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 患者已发送病例列表
     */
    public function bingliSendOkList(Request $request)
    {
        $token = $request->post('token', '');
        $zx_id = $request->post('zx_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->bingliSendOkList($zx_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 患者发送病例
     */
    public function bingliSend(Request $request)
    {
        $token = $request->post('token', '');
        $zxid = $request->post('zx_id', 0);
        $cnt = $request->post('cnt', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$zxid || !$cnt) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->bingliSend($zxid, $uid, $cnt);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}